/**
 * 司机编辑、新增
 *
 */
define(["app", "js/services/MessageBox", "js/services/MuseService", "js/directives/DatetimePicker",
    "js/directives/FileUpload"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取参数
        var id = $stateParams.id || '';
        $scope.driver = {
            enable: true
        };
        $scope.oldPwd = null;

        $scope.licenseTypeList = ["A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3"];
        $scope.authorizedList = [{name: "未通过", value: -1}, {name: "未提交", value: 0}, {
            name: "待审核",
            value: 1
        }, {name: "已审核", value: 2}];

        //获取数据
        $scope.getDetail = function () {
            MuseService.get(baseServices_restfulUrl + "driver/load.json?id=" + id).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.driver = data.data;
                    } else {
                        MessageBox.notify('获取数据失败:' + data.message);
                    }
                })
            .error(function () {
                MessageBox.error('服务器正在开小差zZZ,获取不到数据');
            });
        };
        if (id) {
            $scope.getDetail();
        }

        //保存
        $scope.saveObj = function () {
            if (isEmpty($scope.driver.password)) {
                if (isEmpty($scope.driver.id)) {
                    // 新增用户时没有输入密码
                    MessageBox.notify('请输入密码');
                    return false;
                }
            } else {
                // 把未加密的密码记录下来
                $scope.oldPwd = $scope.driver.password;
                // 密码进行MD5加密
                $scope.driver.password = hex_md5($scope.driver.password);
            }
            MuseService.postObj(baseServices_restfulUrl + "driver/saveFromVO.json", $scope.driver).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify('保存成功!');
                        $state.go('driverList');
                    } else {
                        $scope.driver.password = $scope.oldPwd;
                        MessageBox.notify('保存失败：' + data.message);
                    }
                })
            .error(function () {
                $scope.driver.password = $scope.oldPwd;
                MessageBox.error('保存失败:服务器正在开小差zZZ');
            });
        };

        $scope.goCar = function (driverId) {
            // $state.go('carList', {driverId: driverId});
        }

        // 取消，返回
        $scope.close = function () {
            window.history.go(-1);
        };

        //打开图片预览页面
        $scope.showPic = function (id) {
            var picId = id;
            window.open(baseServices_restfulUrl + "static/views/preview/imgPreview.html?id=" + picId);
        };
    }

    controller.$inject = deps;
    app.lazy.controller("CarDriverEditController", controller);
});
